using System.Collections.Specialized;
using System.ComponentModel;
using EnvDTE;
using Microsoft.Practices.RecipeFramework.Library.Converters;
using Microsoft.Practices.RecipeFramework.Library.Editors;
using Microsoft.Practices.WizardFramework;
using VSSDKAssist.Assets.Converters;
using VSSDKAssist.Recipes.Common.CustomPages;
using VSSDKAssist.Recipes.Common.Helpers;

namespace VSSDKAssist.Recipes.CreateVSCommandVSCT
{
	/// <summary>
	/// Custom page for CreateVSCommand recipe
	/// </summary>
	public partial class CreateVSCommandCP1 : WizardPageBase
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="CreateVSCommandCP1"/> class.
		/// </summary>
		public CreateVSCommandCP1()
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="CreateVSCommandCP1"/> class.
		/// </summary>
		/// <param name="wizard">The <see cref="T:Microsoft.Practices.WizardFramework.WizardForm"/>that will host the new page.</param>
		public CreateVSCommandCP1(WizardForm wizard)
			: base(wizard)
		{
			InitializeComponent();
		}

		/// <summary>
		/// Gets or sets the name of the command.
		/// </summary>
		/// <value>The name of the command.</value>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "value"), RecipeArgument]
		public string CommandName
		{
			get { return (string)veCommandName.Value; }
			set
			{
			}
		}

		/// <summary>
		/// Gets or sets the destination folder.
		/// </summary>
		/// <value>The destination folder.</value>
		[RecipeArgument]
		public string DestinationFolder
		{
			get { return veDestinationFolder.Value.ToString(); }
			set
			{
				if(value != null)
				{
					veDestinationFolder.TextBox.Text = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the VSCT project item.
		/// </summary>
        /// <value>The VSCT project item.</value>
		[RecipeArgument]
		public ProjectItem VSCTProjectItem
		{
			get { return (ProjectItem)veVSCTFile.Value; }
			set
			{
				if(value != null)
				{
					veVSCTFile.TextBox.Text = new ProjectItemConverter().ConvertTo(value, typeof(string)) as string;
				}
			}
		}

		/// <summary>
		/// Gets or sets the bitmap destination folder.
		/// </summary>
		/// <value>The bitmap destination folder.</value>
		[RecipeArgument]
		public ProjectItem BitmapDestinationFolder
		{
			get { return (ProjectItem)veResourcesFolder.Value; }
			set
			{
				if(value != null)
				{
					veResourcesFolder.TextBox.Text = new ProjectItemConverter().ConvertTo(value, typeof(string)) as string;
				}
			}
		}

		/// <summary>
		/// Gets or sets the site of the control.
		/// </summary>
		/// <value></value>
		/// <returns>The <see cref="T:System.ComponentModel.ISite"></see> associated with the <see cref="T:System.Windows.Forms.Control"></see>, if any.</returns>
		/// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
		public override ISite Site
		{
			get
			{
				return base.Site;
			}
			set
			{
				base.Site = value;
				if(value != null)
				{
					this.veCommandName.BeginInit();
					this.veCommandName.ValueType = typeof(string);
					base.Site.Container.Add(veCommandName);
					this.veCommandName.ConverterInstance = new CSharpIdentifierConverter();
					this.veCommandName.EndInit();

					this.veDestinationFolder.BeginInit();
					this.veDestinationFolder.ValueType = typeof(string);
					base.Site.Container.Add(veDestinationFolder);
					this.veDestinationFolder.ConverterInstance = new CSharpIdentifierConverter();
					this.veDestinationFolder.EndInit();

					this.veVSCTFile.BeginInit();
					this.veVSCTFile.ValueType = typeof(ProjectItem);
					base.Site.Container.Add(veVSCTFile);

					SolutionPickerEditor solutionPickerEditor = new SolutionPickerEditor();
					StringDictionary solutionPickerEditorAttributes = new StringDictionary();
                    solutionPickerEditorAttributes.Add("UnboundReferenceType", "VSSDKAssist.Recipes.Common.References.VSCTProjectItemReference, VSSDKAssist");
					solutionPickerEditor.Configure(solutionPickerEditorAttributes);
					this.veVSCTFile.EditorInstance = solutionPickerEditor;

					this.veVSCTFile.ConverterInstance = new ProjectItemConverter();
					this.veVSCTFile.EndInit();

					this.veResourcesFolder.BeginInit();
					this.veResourcesFolder.ValueType = typeof(ProjectItem);
					base.Site.Container.Add(veResourcesFolder);
					SolutionPickerEditor solutionPickerEditor1 = new SolutionPickerEditor();
					StringDictionary solutionPickerEditorAttributes1 = new StringDictionary();
					solutionPickerEditorAttributes1.Add("UnboundReferenceType", "VSSDKAssist.Assets.References.VisualStudio.ProjectFolderReference, VSSDKAssist.Assets");
					solutionPickerEditor1.Configure(solutionPickerEditorAttributes1);
					this.veResourcesFolder.EditorInstance = solutionPickerEditor1;
					this.veResourcesFolder.ConverterInstance = new ProjectItemConverter();
					this.veResourcesFolder.EndInit();
				}
			}
		}

		private void veCtcFile_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			ProjectItem ctcProjectItem = (ProjectItem)veVSCTFile.Value;
			DictionaryService.SetValue("CtcProjectItem", ctcProjectItem);
		}

		private void veCommandName_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			if(veCommandName.ConverterInstance != null)
			{
				errorProvider.SetError(veCommandName, string.Empty);

				if(veCommandName.Value != null)
				{
					if(CustomPageHelper.ProjectItemExists(
						DictionaryService,
						"VSPackageProject",
						"DestinationFolder",
                        string.Concat(veCommandName.Value.ToString(), "Constants", ".", DictionaryService.GetValue("LanguageExtension").ToString())))
					{
						DictionaryService.SetValue("CommandName", null);
						errorProvider.SetError(veCommandName, Properties.Resources.InvalidValue);
					}
					else
					{
						DictionaryService.SetValue("CommandName", veCommandName.Value.ToString());
					}
				}
				else
				{
					DictionaryService.SetValue("CommandName", null);
					errorProvider.SetError(veCommandName, Properties.Resources.InvalidValue);
				}
			}
			else
			{
				DictionaryService.SetValue("CommandName", veCommandName.Value.ToString());
			}
		}

		private void veDestinationFolder_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			if(veDestinationFolder.ConverterInstance != null)
			{
				errorProvider.SetError(veDestinationFolder, string.Empty);

				if(veDestinationFolder.Value != null)
				{
					DictionaryService.SetValue("DestinationFolder", veDestinationFolder.Value.ToString());
				}
				else
				{
					DictionaryService.SetValue("DestinationFolder", null);
					errorProvider.SetError(veDestinationFolder, Properties.Resources.InvalidValue);
				}
			}
			else
			{
				DictionaryService.SetValue("DestinationFolder", veDestinationFolder.Value.ToString());
			}
		}

		private void veResourcesFolder_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			ProjectItem folder = (ProjectItem)veResourcesFolder.Value;
			DictionaryService.SetValue("BitmapDestinationFolder", folder);
		}
	}
}